package com.rc.saas.tenant.service.order;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.base.Splitter;
import com.rc.saas.tenant.mapper.order.OrderDiscountRecordMapper;
import com.rc.saas.tenant.model.order.OrderDiscountRecord;
import com.rc.saas.tenant.search.order.OrderDiscountRecordSearch;
import com.rc.saas.tenant.service.tenant.TenantRoleService;
import com.rc.saas.tenant.vo.member.MemberResultVo;
import com.rc.saas.tenant.vo.order.ExportOrderDiscountRecordVo;
import com.rc.saas.tenant.vo.order.OrderDiscountRecordVo;
import com.rc.saas.tenant.vo.tenant.TenantUserRoleVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Create by Yang on 2021/6/24
 */
@Service
public class OrderDiscountRecordServiceImpl implements OrderDiscountRecordService {

    @Autowired
    private OrderDiscountRecordMapper orderDiscountRecordMapper;


    @Autowired
    private TenantRoleService tenantRoleService;

    @Override
    public OrderDiscountRecordMapper getMapper() {
        return orderDiscountRecordMapper;
    }


    @Override
    public int deleteBatchOrderDiscountRecord(Integer count) {
        List<OrderDiscountRecord> orderDiscountRecords = orderDiscountRecordMapper.orderDiscountRecordList(count);
        if (CollectionUtils.isEmpty(orderDiscountRecords)) {
            return 0;
        }



        for (OrderDiscountRecord orderDiscountRecord : orderDiscountRecords) {
            orderDiscountRecordMapper.deleteByPrimaryKey(orderDiscountRecord.getOrderDiscountId());
        }
        //System.out.println("结束本次处理数据" + orderDiscountRecords.size() + "对应ID:" + orderDiscountRecords.get(0).getOrderDiscountId());

        return orderDiscountRecords.size();
    }

    @Override
    public PageInfo<OrderDiscountRecordVo> searchByCondition(OrderDiscountRecordSearch search) {

        TenantUserRoleVo userRoleVo = tenantRoleService.getLonginRole(search.getLoginName());
        if (userRoleVo!=null){
            search.setRoleType(userRoleVo.getRoleType());
            search.setDataAuth(userRoleVo.getDataAuth());
            switch (userRoleVo.getRoleType()){
                case "1"://自定义用户
                    search.setOperatorIds(
                            StringUtils.isNotBlank(userRoleVo.getOperatorIds())?
                                    Arrays.asList(userRoleVo.getOperatorIds().split(","))
                                    :new ArrayList<String>());
                    search.setEnterpriseIds(
                            StringUtils.isNotBlank(userRoleVo.getEnterpriseIds())?
                                    Arrays.asList(userRoleVo.getEnterpriseIds().split(","))
                                    :new ArrayList<String>());
                    break;
                case "2"://加盟商用户
                    search.setOperatorIds(StringUtils.isNotBlank(userRoleVo.getOperatorIds())?
                            Arrays.asList(userRoleVo.getOperatorIds().split(","))
                            :new ArrayList<String>());
                    break;
                case "3"://企业用户
                    search.setEnterpriseIds(StringUtils.isNotBlank(userRoleVo.getEnterpriseIds())?
                            Arrays.asList(userRoleVo.getEnterpriseIds().split(","))
                            :new ArrayList<String>());
                    break;
            }
        }
        PageHelper.startPage(search.getOffset(), search.getLimit());
        List<OrderDiscountRecordVo> list = orderDiscountRecordMapper.searchByCondition(search);
        return new PageInfo<>(list);
    }

    @Override
    public List<ExportOrderDiscountRecordVo> exportOrderDiscountRecord(OrderDiscountRecordSearch search) {
        return orderDiscountRecordMapper.exportOrderDiscountRecord(search);
    }

    @Override
    public void delOrderDiscountRecord(Long orderDiscountId, String loginName) {
        //List<String> orderDiscountIdList = Splitter.on(",").omitEmptyStrings().trimResults().splitToList(id);
        //for (Long orderDiscountId : idList) {
            //Long orderDiscountId = Long.parseLong(dDid);
            orderDiscountRecordMapper.delOrderDiscountRecord(orderDiscountId, loginName);
        //}
    }


}
